package com.resou.reader.choosegender;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.resou.reader.R;
import com.resou.reader.api.entry.GenderCategory;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChooseBooksAdapter extends RecyclerView.Adapter<ChooseBooksAdapter.ChooseBookHolder> {
    private static final String TAG = "ChooseBooksAdapter";
    private final int textColorUnselected = 0xFF666666;     //灰色
    private final int textColorSelected = 0xFFD53D2D;       //红色

    private SparseArray<String> chosenItems;

    private List<GenderCategory> mCategories;

    public ChooseBooksAdapter() {
        chosenItems = new SparseArray<>();
        mCategories = new ArrayList<>();
    }

    public void setCategories(List<GenderCategory> categories) {
        chosenItems.clear();
        mCategories = categories;
        notifyDataSetChanged();
    }

    String getChosenItems() {
        StringBuilder builder = new StringBuilder();
        Log.d(TAG, "getChosenItems: " + chosenItems.size());
        Log.d(TAG, "getChosenItems: " + chosenItems);
        for (int i = 0; i < chosenItems.size(); i++) {
            String str = chosenItems.valueAt(i);
            builder.append(str);
            //不是最后一个添加逗号
            if (i != chosenItems.size() - 1) {
                builder.append(",");
            }
        }
        return builder.toString();
    }

    @NonNull
    @Override
    public ChooseBookHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        return new ChooseBookHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_item_starter_choose_book, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ChooseBookHolder holder, int position) {
        GenderCategory category = mCategories.get(position);
        if (clearAll) {
            holder.tvBookName.setTextColor(textColorUnselected);
            holder.tvBookName.setSelected(false);
            if (position == getItemCount() - 1) {
                clearAll = false;
            }
        } else {
            if (category.getDefaultSelection() == 1) {
                //默认选中
                chosenItems.put(position, category.getCategoryId());
                holder.tvBookName.setSelected(true);
                holder.tvBookName.setTextColor(textColorSelected);
            } else {
                holder.tvBookName.setSelected(false);
                holder.tvBookName.setTextColor(textColorUnselected);
            }
        }

        holder.tvBookName.setText(category.getCategoryName());

        holder.itemView.setOnClickListener(v -> {
            String categoryId = chosenItems.get(position);
            if (categoryId != null) {
                //从列表中移除此分类
                chosenItems.remove(position);
                holder.tvBookName.setTextColor(textColorUnselected);
                holder.tvBookName.setSelected(false);
            } else {
                //往列表中添加此分类
                chosenItems.put(position, category.getCategoryId());
                holder.tvBookName.setSelected(true);
                holder.tvBookName.setTextColor(textColorSelected);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mCategories == null ? 0 : mCategories.size();
    }

    private boolean clearAll;

    public void clearSelected() {
        clearAll = true;
        chosenItems.clear();
        notifyDataSetChanged();
    }

    class ChooseBookHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_book_name)
        TextView tvBookName;

        ChooseBookHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
